package myreflect4;

import java.lang.reflect.Field;

/*
    利用Field对象，获取值或者修改值
* */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        // object get(object obj)返回由该 Field表示的字段在指定对象上的值。
        // method1();
        // method2();
    }

    private static void method2() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect4.Student");
        // 2.获取成员变量Field的对象
        Field field = clazz.getDeclaredField("money");

        // 3.取消访问检查
        field.setAccessible(true);
        // 4.调用get方法来获取值
        // 创建一个对象
        // Student student = (Student) clazz.newInstance();
        // 获取指定对象的money的值
        // Object o = field.get(student);
        // System.out.println(o);
        System.out.println(field.get(clazz.newInstance()));
    }

    private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        // void set(object obj，object value):给obj对象的成员变量赋值为value
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect4.Student");
        // 2.获取name这个Field对象
        Field field = clazz.getField("name");

        // 3.利用set方法进行赋值
        // 3.1先创建一个student对象
        Student student = (Student) clazz.newInstance();
        // 3.2有了对象才可以给指定对象进行赋值
        field.set(student, "zhangsan");
        System.out.println(student);
    }
}
